﻿using MediatR;
using Traceability.APP.Applications.Commands.Suyuan;
using Traceability.APP.Dtos;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.APP.Applications.CommandHandlers.Suyuan
{
    public class SuyuanXiangqingCommandHandler : IRequestHandler<SuyuanXiangqingCommand, APIResult<SuyuanQuerydto>>
    {
        private readonly IBaseRepository<BasicTraceabilityModel> _syRepository;
        private readonly IBaseRepository<ShopModel> _shopRepository;

        public SuyuanXiangqingCommandHandler(IBaseRepository<BasicTraceabilityModel> syRepository, IBaseRepository<ShopModel> shopRepository)
        {
            _syRepository = syRepository;
            _shopRepository = shopRepository;
        }

        public Task<APIResult<SuyuanQuerydto>> Handle(SuyuanXiangqingCommand request, CancellationToken cancellationToken)
        {
            APIResult<SuyuanQuerydto> result = new APIResult<SuyuanQuerydto>();
            result.Code = APIEnum.QuerySuccess;
            result.Message = APIEnum.QuerySuccess.Desc();
            var m = from a in _syRepository.GetAll()
                    join b in _shopRepository.GetAll()
                    on a.Sid equals b.Sid
                    where a.IsDel == 0
                    select new SuyuanQuerydto
                    {
                        Id = a.Id,
                        Sid = a.Sid,
                        Batch = a.Batch,
                        ShelfLife = a.ShelfLife,
                        ProductionNumber = a.ProductionNumber,
                        QiYe = a.QiYe,
                        Information = a.Information,
                        Img = a.Img,
                        CreateName = a.CreateName,
                        CreateTime = a.CreateTime,
                        UpdateName = a.UpdateName,
                        UpdateTime = a.UpdateTime,
                        SName = b.SName
                    };
            var l = m.FirstOrDefault(x => x.Batch == request.Batch);
            result.Data = l;
            return Task.FromResult(result);
        }
    }
}
